﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.models {
    import flash.events.*;
    import flash.utils.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class CollectableModel extends Actor {

        private var _collectableMatrix:Dictionary;
        private var _completedCollections:Dictionary;
        private var _lastAddedPetType:String;
        private var _numberOfCompletedCollections:int;

        public function CollectableModel(){
            this._collectableMatrix = new Dictionary();
            this.completedCollections = new Dictionary();
        }
        public function addTypeToMatrix(_arg1:CollectableVO):void{
            this._collectableMatrix[_arg1.type] = _arg1;
        }
        public function getTypeFromMatrix(_arg1:String):CollectableVO{
            return ((this._collectableMatrix[_arg1] as CollectableVO));
        }
        public function addCollectables(_arg1:Object):void{
            var _local2:String;
            for (_local2 in _arg1) {
                switch (_local2){
                    case "elder":
                    case "baby":
                        this._collectableMatrix[_local2] = _arg1[_local2];
                        break;
                    default:
                        this.addVO(_local2, _arg1[_local2]);
                        break;
                };
            };
            this.countCompletedCollections();
        }
        private function addVO(_arg1:String, _arg2:Object):void{
            var _local3:CollectableVO = this.getTypeFromMatrix(_arg1);
            if (_local3){
                _local3.collected = (_arg2 as Array);
            };
        }
        public function getCollectableByType(_arg1:String){
            return (this._collectableMatrix[_arg1]);
        }
        public function updateCollectableByType(_arg1:String, _arg2:int, _arg3:String):void{
            var _local4:CollectableVO = this._collectableMatrix[_arg1];
            this._lastAddedPetType = _arg1;
            switch (_arg2){
                case 3:
                    this._collectableMatrix[_arg3] = (this._collectableMatrix[_arg3] + 1);
                    break;
                default:
                    _local4.collected[_arg2] = (_local4.collected[_arg2] + 1);
                    break;
            };
            this.countCompletedCollections();
        }
        private function countCompletedCollections():void{
            var _local1:Object;
            this.numberOfCompletedCollections = 0;
            for each (_local1 in this.collectableMatrix) {
                if ((_local1 is CollectableVO)){
                    if (this.isCollectionComplete((_local1 as CollectableVO))){
                        this.numberOfCompletedCollections++;
                    };
                };
            };
            l(("# of completed collections " + this.numberOfCompletedCollections));
        }
        private function isCollectionComplete(_arg1:CollectableVO):Boolean{
            var _local2:int;
            while (_local2 < (_arg1.requires.length - 1)) {
                if (_arg1.requires[_local2] > _arg1.collected[_local2]){
                    return (false);
                };
                _local2++;
            };
            if (_arg1.requires[3] > this.collectableMatrix[_arg1.rewardPet]){
                return (false);
            };
            return (true);
        }
        private function getGlobalRewardsForType(_arg1:String):int{
            var _local2:CollectableVO = this._collectableMatrix[_arg1];
            return (this._collectableMatrix[_local2.rewardPet]);
        }
        public function tradeInCollection(_arg1:String):void{
            var _local2:CollectableVO = this._collectableMatrix[_arg1];
            var _local3:int;
            while (_local3 < _local2.collected.length) {
                _local2.collected[_local3] = (_local2.collected[_local3] - _local2.requires[_local3]);
                _local3++;
            };
            this.collectableMatrix[_local2.rewardPet] = (this.collectableMatrix[_local2.rewardPet] - _local2.requires[3]);
            if (this.completedCollections.hasOwnProperty(_arg1)){
                delete this.completedCollections[_arg1];
            };
            this.countCompletedCollections();
        }
        public function get collectableMatrix():Dictionary{
            return (this._collectableMatrix);
        }
        public function set collectableMatrix(_arg1:Dictionary):void{
            this._collectableMatrix = _arg1;
        }
        public function get completedCollections():Dictionary{
            return (this._completedCollections);
        }
        public function set completedCollections(_arg1:Dictionary):void{
            this._completedCollections = _arg1;
        }
        public function get lastAddedPetType():String{
            return (this._lastAddedPetType);
        }
        public function set lastAddedPetType(_arg1:String):void{
            this._lastAddedPetType = _arg1;
        }
        public function get numberOfCompletedCollections():int{
            return (this._numberOfCompletedCollections);
        }
        public function set numberOfCompletedCollections(_arg1:int):void{
            this._numberOfCompletedCollections = _arg1;
            dispatch(new Event(EventConstants.COLLECTIONS_UPDATE));
        }

    }
}//package net.wooga.pets.models 
